


#基础
person_info = {
    "name":"Alex",
    "age":30,
    "country":"USA",
    "language":["English","spanish"],
    "is_student":False
}
#1.打印'person_info'字典的'name'值
print(person_info["name"])

#2.修改'age'的值为31
person_info["age"] = 31
print(person_info)

#3.向字典中添加一个新的键值对'height':175
person_info["height"] = 175
print(person_info)

#4.删除字典中的'is_student'键及其值
del person_info["is_student"]
print(person_info)

#5.使用循环遍历字典，打印所有的键和值
for key, value in person_info.items():
    print(f"{key}:{value}")

#6.检查字典中是否有'email'这个键，如果没有，添加'email':"alex@example.com"
i = 0
for key in person_info:
    if key == "email":
        print("字典中有email键")
        i += 1
        break
if i == 0:
    print("字典中没有email键，将添加")
person_info["email"] = "alex@example.com"
print(person_info)

#7.将'language'的值改为只包含'English'
person_info["language"] = ["English"]
print(person_info)

#8.获取并打印字典中'language'列表的长度
print(len(person_info["language"]))

#9.向'language'列表中添加一种新语言'French'
person_info["language"].append("French")
print(person_info)

#10.循环插入新的键值对到字典：将'hobby' + 数字作为键，相应的'hobby' + 数字作为值，数字从1至3
for i in range(4):
    person_info["hobby" + str(i)] = "hobby" + str(i)
print(person_info)


#进阶
employees = {
    101:{
        "name":"Alice",
        "age":30,
        "gender":"Female",
        "position":"Data Scientist",
        "team":"Research",
        "salary_history":[70000, 80000, 90000]
    }
}
print(employees)
#1.新增一个员工的信息到'employees'字典中
employees[102] = {
    "name": "Bob",
    "age": 31,
    "gender": "Male",
    "position": "Data Scientist",
    "team": "Research",
    "salary_history": [70000, 80000, 90000]
}
print(employees)

#2.删除一个指定的员工的信息
del employees[102]
print(employees)

#3.更新员工101的年龄至31
employees[101]["age"] = 31
print(employees)

#4.增加员工101薪资历史中的最新薪资至95000
employees[101]["salary_history"].append(95000)
print(employees)

#5.计算员工101的平均薪资
sum_1 = 0
avg = 0
i = 0
for num in employees[101]["salary_history"]:
    sum_1 += num
    i += 1
avg = sum_1 / i
print(avg)

#6.找出所有年龄大于25岁的员工的姓名和ID
for key in employees:
    if employees[key]["age"] > 25:
        print(f"{employees[key]['name']}:{key}")

#7.对'employees'中的每个员工，添加'bonus'键至字典中，其中值是最新薪资的10%
for key in employees:
    employees[key]["bonus"] = str(employees[key]["salary_history"][-1] / 10)  #这里为什么不能为浮点型
print(employees)

#8将员工101的职位更新为"Senior Data Scientist"
employees[101]["position"] = "Senior Data Scientist"
print(employees)

#9.从员工的信息中移除'gender'键
del employees[101]["gender"]
print(employees)

#10.创建一个新的嵌套字典，该字典仅包含员工的'name'和'position'
employees_1 = {
    101:{
        'name':'Alice',
        'position':'Data Scientist',
    }
}
print(employees_1)













